﻿using System;
using System.Drawing;
using System.Windows.Forms;

using G.Runtime;
using G.Model;

namespace GameControls
{
    public class BoardLinePainter : IDisposable 
    {
        private readonly GameSpaceDef _GameDef;
        private readonly int _SquareSize;
        private readonly UserControl _UserControl;

        public BoardLinePainter(UserControl pUserControl, GameSpaceDef pGameDef, int pSquareSize)
        {
            _GameDef = pGameDef;
            _SquareSize = pSquareSize;
            _UserControl = pUserControl;
            _UserControl.Paint += OnPaint;
        }
        
        public void Dispose()
        {
            _UserControl.Paint -= OnPaint;
        }
        
        public void OnPaint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Black, 1);
            int width = _GameDef.Width;
            int height = _GameDef.Height;
            
            for (int x = 0; x <= width * _SquareSize; x += _SquareSize)
            {
                Point p1 = new Point(x, 0);
                Point p2 = new Point(x, _SquareSize * height);
                e.Graphics.DrawLine(pen, p1, p2);
            }
            
            for (int y = 0; y <= height * _SquareSize; y += _SquareSize)
            {
                Point p1 = new Point(0, y);
                Point p2 = new Point(_SquareSize * width, y);
                e.Graphics.DrawLine(pen, p1, p2);
            }
        }
    }
}
